#   ============================================================================
#   Author: Youhua Xu
#	Date:	2021-12-01
#	
#   ============================================================================

#   Make a build dir for compilation
MDIR := $(shell pwd)
WRKDIR = $(MDIR)/build
BINDIR = $(MDIR)/bin

#	make build & binary dirs
.base:
	if ! [ -e $(WRKDIR) ]; then mkdir $(WRKDIR) ; fi;
	touch $(WRKDIR)/.base;
	if ! [ -e $(BINDIR) ]; then mkdir $(BINDIR) ; fi;
	touch $(BINDIR)/.base;
	# if ! [ -e $(DEBUG) ]; then mkdir $(DEBUG) ; fi;
	# touch $(DEBUG)/.base;
#   ============================================================================
#   Set the source file path
vpath %.c   main:src:test
vpath %.h   include
vpath %.o 	build
vpath .base build

INCLUDES = -I $(MDIR)/include

CC			= gcc
CCFLAG  	= -Wall
OPTFLAG		= -O2 #-ffast-math #( not recognized by intel compiler )
LIBS		= -lm -lgsl -lgslcblas 
LDFLAG      = 

CCFLAG		+= -D_DEBUG_RHS_

ifeq ($(shell uname -s),Darwin)
	LDFLAG	+= -framework Accelerate #(-framework Accelerate is for Mac OSX)
	CC		= gcc
endif

%.o: %.c .base
	cd $(WRKDIR);$(CC) $(OMPFLAG) $(OPTFLAG) $(CCFLAG) $(INCLUDES) -c ../$< -o $*.o

# MAIN
MAIN_REC   = main_rec.o
MAIN_GRAV  = main_grav.o

# DEPS
DEPS	= params.o ode_rec.o output.o ode_grav.o ODE_rec_test.o iniparser.o dictionary.o background.o

all: rec grav

rec: ${MAIN_REC} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

grav: ${MAIN_GRAV} ${DEPS}
	${CC} ${OPTFLAG} ${LDFLAG} $(addprefix build/,$(notdir $^)) ${LIBS} -o $(BINDIR)/$@

####################################
.PHONY:clean tidy run
clean: .base
	rm -rf $(WRKDIR);
tidy:
	make clean; rm -rf $(BINDIR) $(WRKDIR)
